
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * plugin.c
 * Copyright (C) suren 2008 <surenspost@gmail.com>
 * 
 * plugin.c is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * plugin.c is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <config.h>
#include <ctype.h>
#include <libanjuta/anjuta-shell.h>
#include <libanjuta/anjuta-debug.h>
#include <libanjuta/interfaces/ianjuta-iterable.h>
#include <libanjuta/interfaces/ianjuta-document.h>
#include <libanjuta/interfaces/ianjuta-document-manager.h>
#include <libanjuta/interfaces/ianjuta-editor.h>
#include <libanjuta/interfaces/ianjuta-editor-cell.h>
#include <libanjuta/interfaces/ianjuta-editor-language.h>
#include <libanjuta/interfaces/ianjuta-editor-selection.h>
#include <libanjuta/interfaces/ianjuta-editor-assist.h>
#include <libanjuta/interfaces/ianjuta-preferences.h>
#include <libanjuta/interfaces/ianjuta-symbol.h>
#include <libanjuta/interfaces/ianjuta-language.h>

#include "plugin.h"


#define UI_FILE ANJUTA_DATA_DIR"/ui/language_support_python.ui"
#define GLADE_FILE ANJUTA_DATA_DIR"/glade/language_support_python.glade"


static gpointer parent_class;

gboolean IS_WITHIN_STRING,IS_WITHIN_BRACES;

/*  The Editor manipulation functions begins here !  */

static gboolean 
is_iter_within_string ( IAnjutaIterable* iter , gchar ch ){
	return TRUE;
}




/* End of The Editor manipulation functions */




static void
on_sample_action_activate (GtkAction *action, PythonSupportPlugin *plugin)
{
	GObject *obj;
	IAnjutaEditor *editor;
	IAnjutaDocumentManager *docman;
	
	/* Query for object implementing IAnjutaDocumentManager interface */
	obj = anjuta_shell_get_object (ANJUTA_PLUGIN (plugin)->shell,
									  "IAnjutaDocumentManager", NULL);
	docman = IANJUTA_DOCUMENT_MANAGER (obj);
	editor = IANJUTA_EDITOR (ianjuta_document_manager_get_current_document (docman, NULL));

	/* Do whatever with plugin */
	anjuta_util_dialog_info (GTK_WINDOW (ANJUTA_PLUGIN (plugin)->shell),
							 "Document manager pointer is: '0x%X'\n"
							 "Current Editor pointer is: 0x%X", docman,
							 editor);
}

static GtkActionEntry actions_file[] = {
	{
		"ActionFileSample",                       /* Action name */
		GTK_STOCK_NEW,                            /* Stock icon, if any */
		N_("_Sample action"),                     /* Display label */
		NULL,                                     /* short-cut */
		N_("Sample action"),                      /* Tooltip */
		G_CALLBACK (on_sample_action_activate)    /* action callback */
	}
};


/****************************************************************************
*	This is the main part of the Indentation Manager ! 
*	For each Characted being pressed in the editor ,
*		; RET key
*		; colon (:)  key 
* 		; TAB key
*		; ' ' or " " key press 
*		; any other key presses ! 
*
*****************************************************************************/
static void 
char_inserted_handler( 
	IAnjutaEditor *editor, IAnjutaIterable *insert_pos , gchar ch, PythonSupportPlugin *python_plugin )
{
	
 	if( ch == '\n' || ch == '\r' ) {
	 	printf( " An Enter has been pressed \n " ); 
	}
	else if( ch == ':' ) {
		printf( " A Colon has been pressed \n  " );
	}
	else if( ch == '\t' ) {
		printf( " A Tab Key has been pressed \n" );
	}
	else if( ch == '\'' ){
		handle_within_single_quote( iter , ch ) ;
	}
	else if( ch == '\"'){
		
	}
	
		
	 	
	 fflush(stdout);
 	
  	return  ;
}

static void
enable_python_support( PythonSupportPlugin *python_plugin){


	IAnjutaLanguage *language_handler = 
		anjuta_shell_get_interface( ANJUTA_PLUGIN(python_plugin)->shell , IAnjutaLanguage , NULL );

	if( !language_handler )
		return ;

	if( python_plugin->python_support_enabled )	
		return ;

	gchar *language = ianjuta_language_get_name_from_editor( 
		language_handler , IANJUTA_EDITOR_LANGUAGE(python_plugin->current_editor) ,NULL );
		
	if(  g_str_equal( language , "Python" ) ) {	
		python_plugin->language = language;
		g_signal_connect(
			python_plugin->current_editor , "char-added" , G_CALLBACK( char_inserted_handler ) ,
			python_plugin  );
	}
	else {
		python_plugin->python_support_enabled = FALSE ;	
		return ;	
	}

	ianjuta_editor_set_auto_indent ( IANJUTA_EDITOR( python_plugin->current_editor ) , FALSE , NULL );
								    
	python_plugin->python_support_enabled = TRUE ;	
	
	return;
}

static void 
disable_python_support( PythonSupportPlugin *python_plugin ){
	
	if( !python_plugin->python_support_enabled ) 
		return;
	if( python_plugin && ( g_str_equal( python_plugin->language , "Python" ) ) ){
		g_signal_handlers_disconnect_by_func( 
			python_plugin->current_editor , G_CALLBACK( char_inserted_handler ) , python_plugin );
	}
	
	python_plugin->python_support_enabled = FALSE ;
	
	return ;
}


static void
on_language_change( IAnjutaEditor *editor , const gchar *language , PythonSupportPlugin *python_plugin ) {
	disable_python_support( python_plugin );
	enable_python_support( python_plugin );
	return;
}


static void
enable_python_indentation( PythonSupportPlugin *plugin , const gchar *name , const GValue *value, gpointer data ) {

	PythonSupportPlugin *python_plugin;
	IAnjutaDocument *document = IANJUTA_DOCUMENT( g_value_get_object( value ) );
	python_plugin  = ANJUTA_PLUGIN_PYTHON( plugin );
	if( IANJUTA_IS_EDITOR( document ) ) {
		python_plugin ->current_editor = G_OBJECT( document );
	}
	else {
		python_plugin -> current_editor = NULL;
		return ;
	}
	if( IANJUTA_IS_EDITOR( python_plugin -> current_editor ) ) {
		enable_python_support( python_plugin );
	}
	g_signal_connect( python_plugin -> current_editor , "language_changed", 
					  G_CALLBACK(on_language_change), plugin);
	
	return ;
	
}


static void
disable_python_indentation(AnjutaPlugin *plugin,const gchar *name,const GValue *value,gpointer data){
	PythonSupportPlugin *python_plugin;
	return ;
}

static gboolean
language_support_python_activate (AnjutaPlugin *plugin)
{

	AnjutaUI *ui;

	GtkWidget *wid;
	GladeXML *gxml;

	PythonSupportPlugin *python_plugin;
	
	DEBUG_PRINT ("PythonSupportPlugin: Activating PythonSupportPlugin plugin ...");
	python_plugin = ANJUTA_PLUGIN_PYTHON(plugin);

	/* Add all UI actions and merge UI */
	ui = anjuta_shell_get_ui (plugin->shell, NULL);
	
	python_plugin->prefs = anjuta_shell_get_preferences(plugin->shell,NULL);
		
	python_plugin->action_group = 
		anjuta_ui_add_action_group_entries (ui, "ActionGroupFilelanguage_support_python",
											_("Sample file operations"),
											actions_file,
											G_N_ELEMENTS (actions_file),
											GETTEXT_PACKAGE, TRUE,
											python_plugin);
											
	python_plugin->uiid = anjuta_ui_merge (ui, UI_FILE);
	python_plugin->editor_watch_id = 
					anjuta_plugin_add_watch(python_plugin,
											"document_manager_current_editor",
											enable_python_indentation,
											disable_python_indentation,
											python_plugin );
	
	//printf("a watch has been added !"); fflush(stdout);
	/*
	GValue value = {0,};
	g_value_init (&value, G_TYPE_OBJECT);
	//g_value_set_object (&value, doc);
	anjuta_shell_add_value(plugin->shell,"python_automatic_indentation",&value,NULL);
	printf(" a value has been added "); fflush(stdout);
	
	
	gxml = glade_xml_new (GLADE_FILE, "top_widget", NULL);
	wid = glade_xml_get_widget (gxml, "top_widget");
	python_plugin->widget = wid;
	anjuta_shell_add_widget (plugin->shell, wid,
							 "PythonSupportPluginWidget", _("PythonSupportPlugin widget"), NULL,
							 ANJUTA_SHELL_PLACEMENT_BOTTOM, NULL);

	
	g_object_unref (gxml);
	
	*/
	return TRUE;
}

static gboolean
language_support_python_deactivate (AnjutaPlugin *plugin)
{

	AnjutaUI *ui;
	PythonSupportPlugin *python_plugin;
	
	DEBUG_PRINT ("PythonSupportPlugin: DeActivating PythonSupportPlugin plugin ...");
	python_plugin = ANJUTA_PLUGIN_PYTHON(plugin);

	anjuta_shell_remove_widget (plugin->shell, ((PythonSupportPlugin*)plugin)->widget,	NULL);

	anjuta_plugin_remove_watch (plugin,python_plugin->editor_watch_id,TRUE);
	ui = anjuta_shell_get_ui (plugin->shell, NULL);
	anjuta_ui_remove_action_group (ui, ((PythonSupportPlugin*)plugin)->action_group);
	anjuta_ui_unmerge (ui, ((PythonSupportPlugin*)plugin)->uiid);
	

	return TRUE;
}

static void
language_support_python_finalize (GObject *obj)
{
	/* Finalization codes here */
	GNOME_CALL_PARENT (G_OBJECT_CLASS, finalize, (obj));
}

static void
language_support_python_dispose (GObject *obj)
{
	/* Disposition codes */
	GNOME_CALL_PARENT (G_OBJECT_CLASS, dispose, (obj));
}

static void
language_support_python_instance_init (GObject *obj)
{
	PythonSupportPlugin *plugin = (PythonSupportPlugin*)obj;
	plugin->uiid = 0;
	plugin->widget = NULL;
}

static void
language_support_python_class_init (GObjectClass *klass) 
{
	AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
	parent_class = g_type_class_peek_parent (klass);
	plugin_class->activate = language_support_python_activate;
	plugin_class->deactivate = language_support_python_deactivate;
	klass->finalize = language_support_python_finalize;
	klass->dispose = language_support_python_dispose;
}

ANJUTA_PLUGIN_BOILERPLATE (PythonSupportPlugin, language_support_python);
ANJUTA_SIMPLE_PLUGIN (PythonSupportPlugin, language_support_python);
